package com.bhs.sansong.adapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bhs.sansong.R;
import com.bhs.sansong.bean.DiscountCouponBean;
import com.bhs.sansong.utils.MyUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

public class LogisticsDiscountCouponAdapter extends BaseQuickAdapter<DiscountCouponBean, BaseViewHolder> {


    private int type = 0;//大于0过期和已经使用

    public LogisticsDiscountCouponAdapter() {
        super(R.layout.item_logistics_discount_coupon);
    }


    public LogisticsDiscountCouponAdapter(int layoutId, int type) {
        super(layoutId);
        this.type = type;
    }

    public LogisticsDiscountCouponAdapter(List<DiscountCouponBean> list) {
        super(R.layout.item_logistics_discount_coupon, list);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, DiscountCouponBean item) {
        if (!TextUtils.isEmpty(item.getCompanyName())) {
            Glide.with(mContext).load(item.getCompanyImg()).into((ImageView) helper.getView(R.id.iv_cover));
            helper.setText(R.id.tv_title, item.getCompanyName());
            if (type == 0) {
                helper.setTextColor(R.id.tv_title, Color.parseColor("#485EF4"));
            }
        } else {
            helper.setText(R.id.tv_title, item.getUse_type());
            helper.setText(R.id.tv_platform_money, MyUtils.checkPoint(item.getCoupon_price()));
            helper.setText(R.id.tv_platform_tv_satisfy, String.format("满%s减%s", MyUtils.checkPoint(item.getCoupon_min_price()), MyUtils.checkPoint(item.getCoupon_price())));
            if (type == 0) {
                helper.setTextColor(R.id.tv_title, Color.parseColor("#FF6067"));
            }
        }
        helper.setGone(R.id.iv_cover, !TextUtils.isEmpty(item.getCompanyName()));
        helper.setGone(R.id.ll_money, !TextUtils.isEmpty(item.getCompanyName()));
        helper.setGone(R.id.tv_satisfy, !TextUtils.isEmpty(item.getCompanyName()));
        helper.setGone(R.id.ll_platform, TextUtils.isEmpty(item.getCompanyName()));

        helper.setText(R.id.tv_satisfy, String.format("满%s使用", MyUtils.checkPoint(item.getCoupon_min_price())));
        helper.setText(R.id.tv_money, MyUtils.checkPoint(item.getCoupon_price()));
        helper.setText(R.id.tv_available_time, String.format("%s前可用", item.getDate_end()));
        helper.addOnClickListener(R.id.tv_receive_coupon);
        if (item.getGetCouponInfo() == null && item.getCouponUserid() == null) {
            helper.setBackgroundRes(R.id.tv_receive_coupon, item.getCan_take() == 1 ? R.drawable.btn_red_21 : R.drawable.btn_grey_21);
            helper.setText(R.id.tv_receive_coupon, item.getCan_take() == 1 ? "立即领券" : "已领取");
            helper.setTextColor(R.id.tv_receive_coupon, Color.parseColor(item.getCan_take() == 1 ? "#ffffff" : "#909399"));
        } else {
            helper.setBackgroundRes(R.id.tv_receive_coupon, R.drawable.btn_red_21);
            helper.setText(R.id.tv_receive_coupon, "立即使用");
            helper.setTextColor(R.id.tv_receive_coupon, Color.parseColor("#ffffff"));
        }


        TextView tvRule = helper.getView(R.id.tv_rule);
        helper.getView(R.id.ll_rule).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (tvRule.getVisibility() == View.GONE) {
                    tvRule.setVisibility(View.VISIBLE);
                } else {
                    tvRule.setVisibility(View.GONE);
                }
            }
        });
        if (type > 0) {
            helper.setImageResource(R.id.iv_icon, type == 1 ? R.mipmap.ic_used : R.mipmap.ic_have_expired);
        }
    }
}
